//
//  QSNotificationDetailsViewController.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/3/19.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "QSNotificationDetailsViewController.h"
#import "QSHomeInterface.h"
#import "QSWebViewController.h"
@interface QSNotificationDetailsViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong)UIScrollView *scrollView;
@property (nonatomic, strong)UILabel *titleLabel;
@property (nonatomic, strong)UILabel *dateLabel;
@property (nonatomic, strong)UILabel *contentText;
@property (nonatomic, strong)UICollectionView *fileBgView;
@property (nonatomic, strong)NSMutableArray *fileArr;
@end
static NSString *fileCell = @"fileCell";
@implementation QSNotificationDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"消息通知";
    [self configSubView];
    [self getNotiDetail];//请求详情
    
    
}

- (void)configSubView{
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.titleLabel];
    [self.view addSubview:self.dateLabel];
    [self.scrollView addSubview:self.contentText];
    [self.view addSubview:self.fileBgView];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(kRealValue(20));
        make.left.equalTo(self.view).offset(kRealValue(20));
        make.right.equalTo(self.view).offset(- kRealValue(20));
    }];
    [_dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_titleLabel);
        make.top.equalTo(_titleLabel.mas_bottom).offset(kRealValue(13));
    }];
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(kRealValue(20));
        make.right.equalTo(self.view).offset(- kRealValue(20));
        make.top.equalTo(_dateLabel.mas_bottom).offset(kRealValue(25));
        make.bottom.equalTo(self.view).offset(- kRealValue(70));
    }];
    [_contentText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(_scrollView);
        make.bottom.equalTo(_scrollView);
//        make.height.offset(kScreenHeight);
    }];
    [_fileBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(kRealValue(20));
        make.right.equalTo(self.view).offset(- kRealValue(20));
        make.top.equalTo(self.scrollView.mas_bottom).offset(kRealValue(10));
        make.bottom.equalTo(self.view).offset(- kRealValue(10));
    }];
    
}


- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [UILabel labelWithName:@"" font:[UIFont systemFontOfSize:17 weight:1] textColor:[UIColor QSColorBlack434343] textAlignment:NSTextAlignmentCenter];
        _titleLabel.numberOfLines = 0;
    }
    return _titleLabel;
}

- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]init];
//        _scrollView.contentSize = CGSizeMake(kScreenWidth, kScreenHeight * 10);
        //        _scrollView.backgroundColor = [UIColor cyanColor];
        _scrollView.bounces = NO;
    }
    return _scrollView;
}

- (UILabel *)contentText{
    if (!_contentText) {
        _contentText = [UILabel labelWithName:@"" font:[UIFont QSFontOfSize14] textColor:[UIColor QSColorBlack666666] textAlignment:NSTextAlignmentLeft];
        _contentText.preferredMaxLayoutWidth = kScreenWidth - 40;
//        [_contentText setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
        _contentText.numberOfLines = 0;
    }
    return _contentText;
}

- (UILabel *)dateLabel{
    if (!_dateLabel) {
        _dateLabel = [UILabel labelWithName:@"" font:[UIFont QSFontOfSize12] textColor:[UIColor QSColorGray999999] textAlignment:NSTextAlignmentCenter];
    }
    return _dateLabel;
}

- (UICollectionView *)fileBgView{
    if (!_fileBgView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(kRealValue(kScreenWidth - 40) , kRealValue(50));
        
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        layout.minimumLineSpacing = kRealValue(10);
        layout.minimumInteritemSpacing = kRealValue(0);
        layout.collectionView.contentInset = UIEdgeInsetsMake(0,0,0,0);
        _fileBgView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
//        [_fileBgView setCollectionViewLayout:layout];
        _fileBgView.showsVerticalScrollIndicator = YES;
        _fileBgView.backgroundColor = [UIColor QSColorWhiteFFFFFF];
        _fileBgView.delegate = self;
        _fileBgView.dataSource = self;
//        _fileBgView.layer.cornerRadius = 8;
//        _fileBgView.layer.masksToBounds = NO;
//        _fileBgView.layer.shadowColor = [UIColor QSColorGrayA1A1A1].CGColor;
//        _fileBgView.layer.shadowOffset = CGSizeMake(0, 0);
//        _fileBgView.layer.shadowOpacity = 0.2f;
//        _fileBgView.layer.shadowRadius = 10;
        [_fileBgView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:fileCell];
        
    }
    return _fileBgView;
}

#pragma mark - CollectionViewDelegate & CollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.fileArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:fileCell forIndexPath:indexPath];
    cell.backgroundColor = [UIColor QSColorWhiteFFFFFF];
    UILabel *fileLabel = [UILabel labelWithName:@"" font:[UIFont QSFontOfSize14] textColor:[UIColor QSColorBlue3F7DEF] textAlignment:NSTextAlignmentLeft];
    fileLabel.frame = cell.contentView.frame;
    fileLabel.numberOfLines = 0;
    fileLabel.text = [NSString stringWithFormat:@" 附件：%@",self.fileArr[indexPath.row][@"fileName"]];
    
    [cell.contentView addSubview:fileLabel];
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    QSWebViewController *web = [[QSWebViewController alloc] init];
    web.URLString = self.fileArr[indexPath.row][@"fileUrl"];
//    web.hideNavigationBar = hideNavgationBar;
    [self.navigationController pushViewController:web animated:YES];
}

#pragma mark - Request
- (void)getNotiDetail{
    if (_imId) {
        [QSAppKeyWindow showIndeterminateHudWithText:@"加载中"];
        [QSHomeInterface getNotificationDetailWithId:_imId andBlock:^(ResponseMessage * _Nonnull rspStatusAndMessage, NSDictionary * _Nonnull result) {
            [QSAppKeyWindow hideHud];
            if (rspStatusAndMessage.code == kResponseSuccessCode) {
                NSLog(@"%@", result);
                if (result) {
                    [self updateUIWithResult:result];
                }
            }
        }];
    }
}
- (void)updateUIWithResult:(NSDictionary *)result{
    self.titleLabel.text = [result objectForKey:@"imTitle"];
    self.contentText.text = [result objectForKey:@"imContext"];
    _scrollView.contentSize =  CGSizeMake(0, _scrollView.height);
    NSString *dateStr = [NSString stringWithFormat:@"%@", [result objectForKey:@"imCreateTime"]] ;
    ;
    self.dateLabel.text = [[result objectForKey:@"imCreateName"] stringByAppendingFormat:@"  %@", [NSDate dateWithTimestamp:dateStr format:@"yyyy-MM-dd"]];
    self.fileArr = [NSMutableArray arrayWithArray:result[@"fileList"]];
//    [self.fileArr addObjectsFromArray:result[@"fileList"]];
    NSLog(@"文件有%ld个", self.fileArr.count);
    [self.fileBgView reloadData];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
